package com.rrd.dw.mr.gzip.kafka;

import com.rrd.dw.utils.DateUtil;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.hfq.UploadBaseData;
import com.ucredit.babel.common.model.hfq.calendar.UploadCalendarData;
import com.ucredit.babel.common.model.hfq.calendar.UploadCalendarDetail;
import com.ucredit.babel.common.model.hfq.calendar.UploadCalendarRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 好分期日历
 *
 * @author guoqiang
 */

public class HfqCalendarRecordParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 好分期日历
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        UploadCalendarRecord obj = (UploadCalendarRecord) model;
        UploadCalendarData data = obj.getData() == null ? new UploadCalendarData() : obj.getData();
        StringBuilder sb = getBaseInfo(obj, successful);
        UploadBaseData baseData = data.getBaseData();
        sb.append(baseData.getHttpTimestamp() == 0 ? "" : DateUtil.toDateStrMs(new Date(baseData.getHttpTimestamp()))).append(CRT001);
        sb.append(baseData.getUploadInfoOpportunity() == null ? "" : baseData.getUploadInfoOpportunity()).append(CRT001);;
        sb.append(baseData.getVersion() == null ? "" : baseData.getVersion()).append(CRT001);
        sb.append(baseData.getIsAuthorized() == null ? "" : baseData.getIsAuthorized()).append(CRT001);
        sb.append(baseData.getOs() == null ? "" : baseData.getOs()).append(CRT001);

        List<UploadCalendarDetail> appDetails = data.getCalendarList();

        StringBuilder TxSb = new StringBuilder(500);
        for (UploadCalendarDetail app : appDetails) {
            TxSb.delete(0, TxSb.length());
            TxSb.append(app.getTitle() == null ? "" : app.getTitle()).append(CRT001);
            TxSb.append(app.getContent() == null ? "" : app.getContent()).append(CRT001);
            TxSb.append(app.getStartTime() == null ? "" : app.getStartTime()).append(CRT001);
            TxSb.append(app.getEndTime() == null ? "" : app.getEndTime()).append(CRT001);
            TxSb.append(app.getTimezone() == null ? "" : app.getTimezone()).append(CRT001);
            TxSb.append(app.getDuration() == null ? "" : app.getDuration()).append(CRT001);
            TxSb.append(app.getLastModifyTime() == null ? "" : app.getLastModifyTime()).append(CRT001);
            TxSb.append(app.getOrganiser() == null ? "" : app.getOrganiser()).append(CRT001);
            key.set(getKey());
            val.set((sb.toString() + TxSb).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        if (appDetails.size() == 0) {
            key.set(getKey());
            val.set(sb.toString().replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_hfq_calendar_record_inc";
    }

    @Override
    public String getType() {
        return RecordType.HFQ_UPLOAD_CALENDAR_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "好分期日历";
    }
}
